package org.or5e.service.core.sm;

import java.util.HashMap;
import java.util.Map;

import org.or5e.service.core.dao.UserLogin;

public class SessionManagerSPI implements SessionManager {
	static Map<String, DAOSession> sessionPool = null;
//	private Timer timer = null;
//	private SessionManagmentTimer timerTask = new SessionManagmentTimer();
//	private static final int SESSION_VERIFY_PERIOD=10000;
//	private static final int SESSION_TIMEOUT = 3600;
	static {
		sessionPool = new HashMap<String, DAOSession>();
	}
	@Override
	public void init() {
//		timer = new Timer();
//		timer.schedule(timerTask, 0, SESSION_VERIFY_PERIOD);
	}
//	private class SessionManagmentTimer extends TimerTask {
//
//		@Override public void run() {
//			System.out.println("Running Session pool verification");
//			Set<String> keySet = sessionPool.keySet();
//			for (String key : keySet) {
//				Calendar cal = sessionPool.get(key).getCreatedDate();
//				cal.add(Calendar.SECOND, SESSION_TIMEOUT);
//				
//				if(cal.after(Calendar.getInstance())) {
//					System.out.println("Removing :"+key);
//					sessionPool.remove(key);
//				}
//			}
//		}
//	}
	@Override
	public void destroy() {
		if(sessionPool != null && sessionPool.size() > 0) sessionPool.clear();
		sessionPool = null;
	}

	@Override
	public DAOSession getSession(String sessionID) {
		return sessionPool.get(sessionID);
	}

	@Override
	public void createNewSession(String sessionID, UserLogin uLogin) {
		//Remove if the session is alread in the pool
		if(sessionPool.containsKey(sessionID)) {
			sessionPool.remove(sessionID);
		}
		DAOSessionImpl sessionImpl = new DAOSessionImpl();
		sessionImpl.put("USERID", uLogin.getUserID());
		sessionImpl.put("LOGINID", uLogin.getLoginID());
		//add the new object of session
		sessionPool.put(sessionID, sessionImpl);
	}

}
